package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.service.AnnouncementManagementService;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import com.ctrip.framework.apollo.util.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/AnnouncementManagement")
public class AnnouncementManagementResource {

    @Resource
    private AnnouncementManagementService announcementManagementService;


    @GetMapping("/getAllNotice")
    public ResultDto<List<Map<String, Object>>> getAllNotice(QueryModel queryModel) {
        List<Map<String, Object>> list = announcementManagementService.getAllNotice(queryModel);
        return new ResultDto<List<Map<String, Object>>>(list);
    }


    @GetMapping("/getOnlyNotice")
    public ResultDto<List<Map<String, Object>>> getOnlyNotice(QueryModel queryModel) {
        List<Map<String, Object>> list = announcementManagementService.getOnlyNotice(queryModel);
        return new ResultDto<List<Map<String, Object>>>(list);
    }

    /**
     * 编辑以及新增公告
     * @param queryModel
     * @return
     */
    @GetMapping("/editNotice")
    public int editNotice(QueryModel queryModel) {
        if (queryModel.getCondition().get("boardId") == ""){
            return  announcementManagementService.addNewNotice(queryModel);
        }
        else {
            return announcementManagementService.updateNotice(queryModel);
        }
    }

    /**
     * 发布公告
     * @param queryModel
     * @return
     */
    @GetMapping("/publishNotice")
    public int publishNotice(QueryModel queryModel) {
            List<Integer> boardIdList = (List<Integer>) queryModel.getCondition().get("boardId");
            return   announcementManagementService.publishNotice(boardIdList);
    }

    @GetMapping("/deleteNotice")
    public int deleteNotice(QueryModel queryModel) {
            List<Integer> boardIdList = (List<Integer>)queryModel.getCondition().get("boardId");
            return announcementManagementService.deleteNotice(boardIdList);
    }

    /**
     * 将web上的上传文件存储指定位置中
     * @param file
     * @return
     */
    @PostMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(MultipartFile file) {
            return announcementManagementService.fileUpload(file);
    }


}
